import 'dart:convert' show json;

T asT<T>(dynamic value) {
  if (value is T) {
    return value;
  }
  return null;
}

class Address {
  Address({
    this.id,
    this.signer_name,
    this.signer_mobile,
    this.address,
    this.area,
    this.default_addr,
    this.lng,
    this.lat,
  });

  factory Address.fromJson(Map<String, dynamic> jsonRes) => jsonRes == null
      ? null
      : Address(
          id: asT<int>(jsonRes['id']),
          signer_name: asT<String>(jsonRes['signer_name']),
          signer_mobile: asT<String>(jsonRes['signer_mobile']),
          address: asT<String>(jsonRes['address']),
          area: Area.fromJson(asT<Map<String, dynamic>>(jsonRes['area'])),
          default_addr: asT<bool>(jsonRes['default_addr']),
          lat: double.parse(asT<String>(jsonRes['lat'])),
          lng: double.parse(asT<String>(jsonRes['lng'])),
        );

  int id;
  String signer_name;
  String signer_mobile;
  String address;
  Area area;
  bool default_addr;
  bool isBeyond = true;
  double lng;
  double lat;
  String centerId;

  String getStringCity() {
    return "${area.province} ${area.city} ${area.district}";
  }

  Map<String, dynamic> toJson() => <String, dynamic>{
        'id': id,
        'signer_name': signer_name,
        'signer_mobile': signer_mobile,
        'address': address,
        'area': area,
        'default_addr': default_addr,
      };

  @override
  String toString() {
    return json.encode(this);
  }
}

class Area {
  Area({
    this.province,
    this.city,
    this.district,
  });

  factory Area.fromJson(Map<String, dynamic> jsonRes) => jsonRes == null
      ? null
      : Area(
          province: asT<String>(jsonRes['province']),
          city: asT<String>(jsonRes['city']),
          district: asT<String>(jsonRes['district']),
        );

  String province;
  String city;
  String district;

  Map<String, dynamic> toJson() => <String, dynamic>{
        'province': province,
        'city': city,
        'district': district,
      };

  @override
  String toString() {
    return json.encode(this);
  }
}
